package com.inforich.app.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inforich.app.api.dto.DriverDTO;
import com.inforich.app.api.service.IAppDriverService;
import com.inforich.app.component.LoginUserApp;
import com.inforich.appManage.domain.AppUser;
import com.inforich.appManage.domain.Contact;
import com.inforich.appManage.service.IAppRoleService;
import com.inforich.appManage.service.IAppUserService;
import com.inforich.appManage.service.IContactService;
import com.inforich.carManage.domain.Driver;
import com.inforich.carManage.service.IDriverService;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.RoleCodeEnum;
import com.inforich.orderManage.domain.Transport;
import com.inforich.orderManage.service.ITransportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2022/10/31
 */
@Service
public class AppDriverServiceImpl implements IAppDriverService {

  @Autowired
  private IDriverService driverService;
  @Autowired
  private IContactService contactService;
  @Autowired
  private ITransportService transportService;
  @Autowired
  private IAppRoleService appRoleService;
  @Autowired
  private IAppUserService appUserService;

  @Override
  public AjaxResult addDriver(LoginUserApp loginUserApp, DriverDTO dto) {
    // 公司ID
    String transportCompanyId = contactService.getTransportCompanyIdByAppUserId(
        loginUserApp.getId());
    if (StrUtil.isBlank(transportCompanyId)) {
      return AjaxResult.error("缺少公司信息");
    }
    int count = driverService.count(
        new LambdaQueryWrapper<Driver>().eq(Driver::getCarNumber, dto.getCarNumber()));
    if (count > 0) {
      return AjaxResult.error("车牌号重复或已被他人使用");
    }
    int countP = driverService.count(
        new LambdaQueryWrapper<Driver>().eq(Driver::getPhone, dto.getPhone()));
    if (countP > 0) {
      return AjaxResult.error("手机号重复或已被他人使用");
    }
    int countC = driverService.count(
        new LambdaQueryWrapper<Driver>().eq(Driver::getIdnumber, dto.getIdnumber()));
    if (countC > 0) {
      return AjaxResult.error("身份证重复或已被他人使用");
    }
    Driver driver = new Driver();
    BeanUtils.copyProperties(dto, driver);
    driver.setTransportCompanyId(transportCompanyId);
    driverService.save(driver);

    // 删除司机后再次添加，如果关系表有数据去更新一下driverId
    AppUser appUser = appUserService.getByIdnumber(dto.getIdnumber());
    if (appUser != null) {
      String roleId = appRoleService.getIdByRoleCode(RoleCodeEnum.DRIVER.getRoleCode());
      contactService.update(new LambdaUpdateWrapper<Contact>()
          .set(Contact::getDriverId, driver.getId())
          .set(Contact::getTransportCompanyId, transportCompanyId)
          .eq(Contact::getAppUserId, appUser.getId())
          .eq(Contact::getAppRoleId, roleId));
    }
    return AjaxResult.success();
  }

  @Override
  public AjaxResult updateDriver(LoginUserApp loginUserApp, DriverDTO dto) {
    // 公司ID
    String transportCompanyId = contactService.getTransportCompanyIdByAppUserId(
        loginUserApp.getId());
    Long id = dto.getId();
    String transportPermitImage = dto.getTransportPermitImage();
    Driver driver = new Driver();
    driver.setId(id);
    driver.setTransportPermitImage(transportPermitImage);
    driverService.update(new LambdaUpdateWrapper<Driver>().set(Driver::getTransportPermitImage,
            dto.getTransportPermitImage())
        .eq(Driver::getId, id)
        .eq(Driver::getTransportCompanyId, transportCompanyId));
    return AjaxResult.success();
  }

  @Override
  public AjaxResult removeDriver(LoginUserApp loginUserApp, Long id) {
    // 公司ID
    String transportCompanyId = contactService.getTransportCompanyIdByAppUserId(
        loginUserApp.getId());
    if (StrUtil.isBlank(transportCompanyId)) {
      return AjaxResult.error("缺少公司信息");
    }
    Driver driver = driverService.getById(id);
    if (!transportCompanyId.equals(driver.getTransportCompanyId())) {
      return AjaxResult.error("警告:只能删除本公司的司机信息");
    }
    int count = transportService.count(
        new LambdaQueryWrapper<Transport>().eq(Transport::getDriverId, id));
    if (count > 0) {
      return AjaxResult.error("该司机已分配订单，不可删除");
    }
    driverService.removeById(id);
    return AjaxResult.success();
  }

  @Override
  public AjaxResult getDriver(LoginUserApp loginUserApp, Long id) {
    // 公司ID
    String transportCompanyId = contactService.getTransportCompanyIdByAppUserId(
        loginUserApp.getId());
    Driver driver = driverService.getOne(new LambdaQueryWrapper<Driver>()
        .eq(Driver::getId, id)
        .eq(Driver::getTransportCompanyId, transportCompanyId));
    return driver == null ? AjaxResult.error("没有相关的车辆信息") : AjaxResult.success(driver);
  }
}
